package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地址簿管理
 */
@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<AddressBook> save(@RequestBody AddressBook addressBook) {

        addressBook.setUserId(BaseContext.getCurrentId());

        log.info("addressBook: {}", addressBook);

        addressBookService.save(addressBook);

        return R.success(addressBook);
    }

    /**
     * 设置默认地址功能实现
     * @param addressBook
     * @return
     */
    @PutMapping("default")
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook) {

        addressBook.setUserId(BaseContext.getCurrentId());

        log.info("addressBook: {}", addressBook);

        // 用户只能有一个默认地址，所以设置默认地址前需要将所有的地址改为非默认地址，重新设置
        // 构造条件
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        // 确定查询条件
        wrapper.eq(AddressBook::getUserId, addressBook.getUserId());
        // 设置为非默认地址
        wrapper.set(AddressBook::getIsDefault, 0);
        // 调用 service 进行修改
        addressBookService.update(wrapper);

        // 进行默认地址设置
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);

        return R.success(addressBook);
    }

    /**
     * 查询用户 id 地址信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> get(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);

        if (addressBook == null) {
            // 为空，说明用户未有地址信息
            return R.error("没有找到该对象");
        } else {
            // 不为空， 说明查询到用户对应的地址
            return R.success(addressBook);
        }

    }

    /**
     * 获取默认地址
     * @return
     */
    @GetMapping("default")
    public R<AddressBook> getDefault() {

        // 构造查询条件
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        // 构造等值查询
        queryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        queryWrapper.eq(AddressBook::getIsDefault, 1);

        // 查询
        AddressBook addressBook = addressBookService.getOne(queryWrapper);

        if (addressBook == null) {
            return R.error("没有找到该对象");
        } else {
            return R.success(addressBook);
        }

    }

    /**
     * 查询指定用户的全部地址
     * @param addressBook
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(AddressBook addressBook) {
        // 指定当前用户 id
        addressBook.setUserId(BaseContext.getCurrentId());

        log.info("addressBook: {}", addressBook);

        // 条件构造器
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(addressBook.getUserId() != null, AddressBook::getUserId, addressBook.getUserId());
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);

        return R.success(addressBookService.list(queryWrapper));
    }

    /**
     * 实现修改地址功能
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<AddressBook> update(@RequestBody AddressBook addressBook) {

        log.info("addressBook: {}", addressBook);

        addressBookService.updateById(addressBook);

        return R.success(addressBook);
    }

    /**
     * 根据 id 实现删除地址
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteById(Long ids) {

        log.info("ids: {}", ids);

        // 构造条件
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getId, ids);

        // 删除操作
        addressBookService.remove(queryWrapper);

        return R.success("删除成功");
    }

}
